package com.yijiu.trb;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.yijiu.network.StringUtils;
import com.yijiu.resource.CacheData;

/**
 * 账户管理
 * 
 * @author TangChao_Lenovo
 */
public class AccountManagerActivity extends BTitleWithUserinfoActivity implements OnClickListener
{
	/**
	 * 佣金管理
	 */
	private TextView managerment_money_button;

	/**
	 * 投资人管理
	 */
	private TextView managerment_investor_buton;

	/**
	 * 投资人开户
	 */
	private TextView openAccount_investor_button;

	/**
	 * 修改登录密码
	 */
	private TextView modify_password_button;

	/**
	 * 绑定手机
	 */
	private TextView bindingToMobileButton;

	/**
	 * 绑定银行卡
	 */
	private TextView bindingToCardButton;

	/**
	 * 实名认证
	 */
	private TextView identificationRealNameButton;

	private long exitTime = 0;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		getWindow().setFormat(PixelFormat.RGBA_8888);
		setContentView(R.layout.activity_account_operation);
		initWidget();
		refresh();
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		refresh();
	}

	@Override
	protected void onRestart()
	{
		super.onRestart();
		refresh();
	}

	@Override
	protected void initWidget()
	{
		super.initWidget();
		managerment_money_button = (TextView) findViewById(R.id.tv_account_operation_money);
		managerment_investor_buton = (TextView) findViewById(R.id.tv_account_operation_money_man);
		openAccount_investor_button = (TextView) findViewById(R.id.tv_account_operation_money_man_open);
		modify_password_button = (TextView) findViewById(R.id.tv_account_operation_pwd);
		bindingToMobileButton = (TextView) findViewById(R.id.tv_account_operation_phone);
		bindingToCardButton = (TextView) findViewById(R.id.tv_account_operation_card);
		identificationRealNameButton = (TextView) findViewById(R.id.identificationRealName);
		if (CacheData.ROLE.equals("12"))
		{
			// 投资人
			managerment_money_button.setVisibility(View.GONE);
			managerment_investor_buton.setVisibility(View.GONE);
			openAccount_investor_button.setVisibility(View.GONE);
		}
		else
		{
			managerment_money_button.setVisibility(View.VISIBLE);
			managerment_investor_buton.setVisibility(View.VISIBLE);
			openAccount_investor_button.setVisibility(View.VISIBLE);
		}
		managerment_money_button.setOnClickListener(this);
		managerment_investor_buton.setOnClickListener(this);
		openAccount_investor_button.setOnClickListener(this);
		modify_password_button.setOnClickListener(this);
		bindingToMobileButton.setOnClickListener(this);
		bindingToCardButton.setOnClickListener(this);
		identificationRealNameButton.setOnClickListener(this);
		showPersonalInfo(CacheData.REAL_NAME,
				(CacheData.AVAILABLE_BALANCE == null || "null".equals(CacheData.AVAILABLE_BALANCE) ? "0.0"
						: CacheData.AVAILABLE_BALANCE));
		setTitleInfoVisibility();
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
			case R.id.tv_account_operation_money:// 佣金管理
				initIntent(this, MoneyManagerActivity.class);
				break;
			case R.id.tv_account_operation_money_man:// 投资人管理
				initIntent(this, InvesterManagerActivity.class);
				break;
			case R.id.tv_account_operation_money_man_open:// 投资人开户
				initIntent(this, RegistActivity.class, "flag", "moneyMan");
				break;
			case R.id.tv_account_operation_pwd:// 修改密码
				initIntent(this, ChangePasswordActivity.class);
				break;
			case R.id.tv_account_operation_phone:// 绑定或修改手机
				String phoneNum = CacheData.MOBILE_NUMBER;
				String flag = "";
				if (!StringUtils.isNull(phoneNum))
				{
					flag = "change";
				}
				else
				{
					flag = "bind";
				}
				initIntent(this, BindToPhoneActivity.class, "flag", flag);
				break;
			case R.id.tv_account_operation_card:// 绑定银行卡
				String realNameStatus = CacheData.REAL_NAME_STATUS;
				if (realNameStatus.equals("N"))
				{
					dialog = new AlertDialog.Builder(this).setTitle("请实名认证")
							.setPositiveButton("确定", new DialogInterface.OnClickListener()
							{
								@Override
								public void onClick(DialogInterface dialog, int which)
								{
									initIntent(AccountManagerActivity.this, RealInfoActivity.class);
								}
							}).create();
					dialog.show();
					return;
				}
				initIntent(this, MyBankCardActivity.class);
				break;
			case R.id.identificationRealName:// 实名认证
				if (CacheData.REAL_NAME_STATUS.equals("IS"))
				{
					Toast.makeText(this, "实名认证已通过", Toast.LENGTH_SHORT).show();
				}
				else if (CacheData.REAL_NAME_STATUS.equals("IN"))
				{
					Toast.makeText(this, "实名认证中", Toast.LENGTH_SHORT).show();
				}
				else
				{
					initIntent(this, RealInfoActivity.class);
				}
				break;
			default:
				break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN)
		{
			if ((System.currentTimeMillis() - exitTime) > 2000)
			{
				Toast.makeText(getApplicationContext(), "再按一次返回键退出程序", Toast.LENGTH_SHORT).show();
				exitTime = System.currentTimeMillis();
			}
			else
			{
				finish();
				System.exit(0);
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
